<?php

    class itTemplateIntelligencerTest extends itTestCase {
        
        /**
         * @test
         */
        public function testAddReturn() {
            $i = new itTemplateIntelligencer('Test');
            $t = new MockTemplate();
            $this->assertEquals($i, $i->addTemplate($t));
            $i = new itTemplateIntelligencer('Test2');
            $this->assertEquals($t, $i->addTemplateAndCreate($t));
        }
        
        /**
         * @test
         */
        public function testNullPriorities() {
            $i = new itTemplateIntelligencer('TestNullPriorities');
            $i->addTemplate(new MockTemplate1())->addTemplate(new MockTemplate2());
            $this->assertTrue($i->load('TemplateIntelligencerA'));
            $a = new TemplateIntelligencerA();
            $this->assertEquals('TemplateIntelligencerA1.php', $a->getFileName());
        }
        
        /**
         * @test
         */
        public function testFirstNullPriority() {
            $i = new itTemplateIntelligencer('TestNullPriorities');
            $i->addTemplate(new MockTemplate1())->addTemplate(new MockTemplate2(), 100);
            $this->assertTrue($i->load('TemplateIntelligencerB'));
            $a = new TemplateIntelligencerB();
            $this->assertEquals('TemplateIntelligencerB2.php', $a->getFileName());
        }
        
        /**
         * @test
         */
        public function testTwoNotNullPriorities() {
            $i = new itTemplateIntelligencer('TestNullPriorities');
            $i->addTemplate(new MockTemplate1(), 50)->addTemplate(new MockTemplate2(), 10);
            $this->assertTrue($i->load('TemplateIntelligencerC'));
            $a = new TemplateIntelligencerC();
            $this->assertEquals('TemplateIntelligencerC2.php', $a->getFileName());
        }
        
        /**
         * @test
         */
        public function testSetGetCacheValues() {
            $i = new itTemplateIntelligencer('hehe');
            $this->assertType('itLocalCache', $i->getCache());
            $this->assertFalse($i->isAggressiveCache());
            $this->assertNull($i->setCache(null)->getCache());
            $this->assertTrue($i->setAggressiveCache(true)->isAggressiveCache());
        }
        
        
        public function testNoCache() {
            $dir = TEST_DATA.'/Individual/Intelligencer/Template/NoCache';
            $class = 'TemplateNoCache_A';
            $i = new itTemplateIntelligencer('ABC');
            $i->addTemplate(new itSimpleTemplate("$dir/%class%.%ext%", array('ext' => 'php')));
            $i->setCache(null);
            $this->assertTrue($i->load($class));
            $this->assertTrue(class_exists($class));
            $this->assertNull($i->getCache());
        }
        
        public function testLocalCache() {
            $dir = TEST_DATA.'/Individual/Intelligencer/Template/LocalCache';
            $class = 'TemplateLocalCache_A';
            $class2 = 'TemplateLocalCache_B';
            $i = new itTemplateIntelligencer('DEF');
            $i->addTemplate(new itSimpleTemplate("$dir/%class%.%ext%", array('ext' => 'php')));
            $this->assertTrue($i->load($class));
            $this->assertTrue(class_exists($class));
            $this->assertNotNull($i->getCache()->get('DEF__templatelocalcache_a'));
            $i->getCache()->set('DEF__templatelocalcache_b', new itPersistentCacheablePrimitive('fake location'));
            $this->assertTrue($i->load($class2));
            $this->assertTrue(class_exists($class2));
            $this->assertNotEquals($i->getCache()->get('DEF__templatelocalcache_b')->getValue(), 'fake location');
        }
        
        public function testPersistentCache() {
            $dir = TEST_DATA.'/Individual/Intelligencer/Template/PersistentCache';
            $class = 'TemplatePersistentCache_A';
            $i = new itTemplateIntelligencer('ZYX');
            itUtil::unlink("$dir/cache.php");
            $i->addTemplate(new itSimpleTemplate("$dir/%class%.%ext%", array('ext' => 'php')));
            $cache = new itPersistentCache("$dir/cache.php");
            $i->setCache($cache);
            $this->assertTrue($i->load($class));
            $this->assertTrue(class_exists($class));
            $this->assertTrue(file_exists("$dir/cache.php"));
            $this->assertNotNull($i->getCache()->get('ZYX__templatepersistentcache_a'));
            itUtil::unlink("$dir/cache.php");
        }
        
        public function testAggressiveLocalCache() {
            $dir = TEST_DATA.'/Individual/Intelligencer/Template/AggressiveLocalCache';
            $class = 'TemplateAggressiveLocalCache_A';
            $class2 = 'TemplateAggressiveLocalCache_B';
            $i = new itTemplateIntelligencer('DEF');
            $i->addTemplate(new itSimpleTemplate("$dir/%class%.%ext%", array('ext' => 'php')));
            $i->setCache(new itLocalCache())->setAggressiveCache(true);
            $this->assertTrue($i->load($class));
            $this->assertTrue(class_exists($class));
            $this->assertNotNull($i->getCache()->get('DEF__templateaggressivelocalcache_a'));
            $i->getCache()->set('DEF__templateaggressivelocalcache_b', new itPersistentCacheablePrimitive('fake location'));
            $this->assertFalse($i->load($class2));
            $this->assertFalse(class_exists($class2));
            $this->assertEquals($i->getCache()->get('DEF__templateaggressivelocalcache_b')->getValue(), 'fake location');
        }
        
        public function testAggressiveLocalCacheUpdatesIfNotExist() {
            $dir = TEST_DATA.'/Individual/Intelligencer/Template/AggressiveLocalCache';
            $class = 'TemplateAggressiveLocalCache_FAKE';
            $i = new itTemplateIntelligencer('FAKE');
            $i->addTemplate(new itSimpleTemplate("$dir/%class%.%ext%", array('ext' => 'php')));
            $i->setCache(new itLocalCache())->setAggressiveCache(true);
            $this->assertFalse($i->load($class));
            $this->assertFalse(class_exists($class));
            $this->assertNull($i->getCache()->get('FAKE__templateaggressivelocalcache_fake')->getValue());
            $this->assertFalse($i->load($class));
            $this->assertFalse(class_exists($class));
        }
        
    }
    
    class MockTemplate extends itTemplate {
        public function getPath($className) {
            list($namespace, $class) = itUtil::getClassAndNS($className);
            return TEST_DATA.'/Individual/Intelligencer/Template/'.$class.'.php';
        }
    }
    
    class MockTemplate1 extends itTemplate {
        public function getPath($className) {
            list($namespace, $class) = itUtil::getClassAndNS($className);
            return TEST_DATA.'/Individual/Intelligencer/Template/'.$class.'1.php';
        }
    }
    
    class MockTemplate2 extends itTemplate {
        public function getPath($className) {
            list($namespace, $class) = itUtil::getClassAndNS($className);
            return TEST_DATA.'/Individual/Intelligencer/Template/'.$class.'2.php';
        }
    }
?>